﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RPG
{
    public static class Util
    {
        public static Vector3[] GetVertices(Model model)
        {
            if (model.Tag == null) throw new NotImplementedException();
            return (Vector3[]) ((Dictionary<string, object>) model.Tag)["vertices"];
        }

        public static BoundingBox GetBoundingBox(Model model)
        {
            if (model.Tag == null) throw new NotImplementedException();
            return (BoundingBox) ((Dictionary<string, object>) model.Tag)["boundingbox"];
        }

        public static BoundingSphere GetBoundingSphere(Model model)
        {
            if (model.Tag == null) throw new NotImplementedException();
            return (BoundingSphere) ((Dictionary<string, object>) model.Tag)["boundingsphere"];
        }

        public static bool VectorIsInBoundingBox(Vector3 vector, BoundingBox bounds)
        {
            if (vector.X > bounds.Min.X && vector.Y > bounds.Min.Y && vector.Z > bounds.Min.Z &&
                vector.X < bounds.Max.X && vector.Y < bounds.Max.Y && vector.Z < bounds.Max.Z) return true;

            return false;
        }

        /*public static Ray GetCursorRay(CGameCamera camera, Viewport viewport, MouseState mousestate)
        {
            Vector3 nearSource = new Vector3(new Vector2(mousestate.X, mousestate.Y), 0f);
            Vector3 farSource = new Vector3(new Vector2(mousestate.X, mousestate.Y), 1f);

            Vector3 nearPoint = viewport.Unproject(nearSource, 
                                                   camera.GetProjectionMatrix(), 
                                                   camera.GetViewMatrix(), 
                                                   Matrix.Identity);

            Vector3 farPoint = viewport.Unproject(farSource, 
                                                  camera.GetProjectionMatrix(), 
                                                  camera.GetViewMatrix(), 
                                                  Matrix.Identity);

            Vector3 direction = farPoint - nearPoint;
            direction.Normalize();

            return new Ray(nearPoint, direction);
        }*/

        public static void ModelActivateLightning(Model whichModel)
        {
            foreach (ModelMesh mesh in whichModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                }
            }
        }

        public static bool KeyPressed(Keys whichKey, KeyboardState oldKS, KeyboardState newKS)
        {
            return oldKS.IsKeyDown(whichKey) == false && newKS.IsKeyDown(whichKey) == true;
        }
    }
}